package com.mhjy.input.service.Impl;

import com.mhjy.input.repository.entity.NhSoDetailEntity;
import com.mhjy.input.service.IInputService;
import com.mhjy.input.service.INhMbMemberService;
import com.mhjy.input.repository.entity.NhMbMemberEntity;
import com.mhjy.input.repository.entity.NhSoMasterEntity;
import com.mhjy.input.repository.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLDataException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;


@Service
public class InputService implements IInputService {


    @Autowired
    private INhMbMemberService nhMbMemberService;

    @Autowired
    private NhSoDetailService soDetailService;

    @Autowired
    private NhSoMasterService soMasterService;

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class, SQLException.class, SQLDataException.class})
    public BaseResultModel GetNHMBMember() {

        BaseResultModel nhm = nhMbMemberService.selectAll();
        if (!nhm.getCode().equals(0))
            return new BaseResultModel(nhm.getCode(), nhm.getMessage());

        List<SimpleNhMbMemberModel> models = new ArrayList<>();
        for (NhMbMemberEntity entity : (List<NhMbMemberEntity>) nhm.getData()) {
            SimpleNhMbMemberModel model = new SimpleNhMbMemberModel();
            model.setMbNo(entity.getMbNo());
            model.setMbName(entity.getMbName());
            model.setOrgId(entity.getOrgId());
            models.add(model);
        }
        return new BaseResultModel(0, "", models);
    }

    @Override
    public BaseResultModel InputMember(List<MbMemberInputModel> models) {

        for (MbMemberInputModel input : models) {
            NhMbMemberEntity entity = new NhMbMemberEntity();
            entity.setAddress(input.getAddress());
            entity.setBirthday(input.getBirthday());
            entity.setMbName(input.getMbName());
            entity.setMbNo(input.getMbNo());
            entity.setCareLevel(input.getCareLevel());
            entity.setIdCardNo(input.getIdCardNo());
            entity.setId(100000L + new Random().nextInt(5));//暂时的
            entity.setOrgId(input.getOrgId());
            entity.setSex(input.getSex());
            entity.setStatus(input.getStatus());
            entity.setInstId(input.getOrgId());
            entity.setMbType("02");
            BaseResultModel k = nhMbMemberService.add(entity);
        }
        return new BaseResultModel(0, "添加成功");
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class, SQLException.class, SQLDataException.class})
    public BaseResultModel InsertSo(InsertSoModel insertSoModel) {

        //master
        NhSoMasterEntity soMasterEntity = new NhSoMasterEntity();
        soMasterEntity.setEntryClass("N");
        soMasterEntity.setEntryTime(new Date());
        soMasterEntity.setEntryBy("10000000610");
        soMasterEntity.setLastUpdatedBy("admin");
        soMasterEntity.setLastUpdatedTime(new Date());
        soMasterEntity.setComments(insertSoModel.getSoMaster().getComments());
        soMasterEntity.setDiscountOffAmt(insertSoModel.getSoMaster().getDiscountOffAmt());
        soMasterEntity.setLogisticType("03");
        soMasterEntity.setMbName(insertSoModel.getSoMaster().getMbName());
        soMasterEntity.setMbNo(insertSoModel.getSoMaster().getMbNo());
        soMasterEntity.setOrgId(insertSoModel.getSoMaster().getOrgId());
        soMasterEntity.setPayBy(insertSoModel.getSoMaster().getPayBy());
        soMasterEntity.setReceiptNo(insertSoModel.getSoMaster().getReceiptNo());
        soMasterEntity.setRefBatchAppNo(insertSoModel.getSoMaster().getRefBatchAppNo());
        soMasterEntity.setRefSoNo(insertSoModel.getSoMaster().getRefSoNo());
        soMasterEntity.setSoAmt(insertSoModel.getSoMaster().getSoAmt());
        soMasterEntity.setSoDate(insertSoModel.getSoMaster().getSoDate());
        soMasterEntity.setSoPeriod(insertSoModel.getSoMaster().getSoPeriod());
        soMasterEntity.setSoStatus("04");
        soMasterEntity.setSoRetailAmt(insertSoModel.getSoMaster().getSoRetailAmt());
        soMasterEntity.setSoType(insertSoModel.getSoMaster().getSoType());
        soMasterEntity.setRefSoDepositNo(insertSoModel.getSoMaster().getRefSoDepositNo());
        soMasterEntity.setRemarks(insertSoModel.getSoMaster().getRemarks());
        soMasterEntity.setSoNo(insertSoModel.getSoMaster().getSoNo());
        soMasterService.add(soMasterEntity);

        //detail
        NhSoDetailEntity soDetailEntity = new NhSoDetailEntity();
        soDetailEntity.setComments(insertSoModel.getSoDetail().getComments());
        soDetailEntity.setDiscountOffAmt(insertSoModel.getSoDetail().getDiscountOffAmt());
        soDetailEntity.setLastUpdatedBy("admin");
        soDetailEntity.setLastUpdatedTime(new Date());
        soDetailEntity.setLineNo(insertSoModel.getSoDetail().getLineNo());
        soDetailEntity.setSoNo(insertSoModel.getSoDetail().getSoNo());
        soDetailEntity.setSoDesc(insertSoModel.getSoDetail().getSoDesc());
        soDetailEntity.setSoQty(insertSoModel.getSoDetail().getSoQty());
        soDetailEntity.setOrgId(insertSoModel.getSoDetail().getOrgId());
        soDetailEntity.setProductCode(insertSoModel.getSoDetail().getProductCode());
        soDetailEntity.setProductPrice(insertSoModel.getSoDetail().getProductPrice());
        soDetailEntity.setProductDesc(insertSoModel.getSoDetail().getProductDesc());
        soDetailEntity.setProductPeriod(insertSoModel.getSoDetail().getProductPeriod());
        soDetailEntity.setProductEndDate(insertSoModel.getSoDetail().getProductEndDate());
        soDetailEntity.setProductStartDate(insertSoModel.getSoDetail().getProductStartDate());
        soDetailEntity.setProductType(insertSoModel.getSoDetail().getProductType());
        soDetailEntity.setSoPrice(insertSoModel.getSoDetail().getSoPrice());
        soDetailEntity.setRemarks(insertSoModel.getSoDetail().getRemarks());
        soDetailService.add(soDetailEntity);

        return new BaseResultModel(0, "添加成功");
    }

}
